package com.das.mechanic_base.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.das.mechanic_base.R;

public class X3SwitchButtonView extends RelativeLayout implements View.OnClickListener {

    private Context mContext;

    private TextView tv_one;
    private TextView tv_two;

    private boolean isOne = true;

    public X3SwitchButtonView(Context context) {
        this(context,null);
    }

    public X3SwitchButtonView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initView();
    }

    private void initView() {
        LayoutInflater.from(mContext).inflate(R.layout.x3_switch_button, this);
        tv_one = findViewById(R.id.tv_one);
        tv_two = findViewById(R.id.tv_two);

        tv_one.setOnClickListener(this);
        tv_two.setOnClickListener(this);
    }

    public void setShowText(String oneText,String twoText) {
        tv_one.setText(oneText);
        tv_two.setText(twoText);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tv_one) {
            if (isOne) {
                return;
            }
            tv_one.setTextColor(ContextCompat.getColor(mContext, R.color.c333));
            tv_two.setTextColor(ContextCompat.getColor(mContext, R.color.cb1b3bd));
            tv_one.setBackground(ContextCompat.getDrawable(mContext, R.drawable.x3_dialog_bg));
            tv_two.setBackground(null);

            if (iOnSwitch != null) {
                iOnSwitch.iOnSwitchOne();
            }
            isOne = true;
        } else if (id == R.id.tv_two) {
            if (!isOne) {
                return;
            }
            tv_two.setTextColor(ContextCompat.getColor(mContext, R.color.c333));
            tv_one.setTextColor(ContextCompat.getColor(mContext, R.color.cb1b3bd));
            tv_two.setBackground(ContextCompat.getDrawable(mContext, R.drawable.x3_dialog_bg));
            tv_one.setBackground(null);
            if (iOnSwitch != null) {
                iOnSwitch.iOnSwitchTwo();
            }
            isOne = false;
        }
    }

    public void setSelect(int position) {
        if (position == 0) {
            isOne = true;
            tv_one.setTextColor(ContextCompat.getColor(mContext,R.color.c333));
            tv_two.setTextColor(ContextCompat.getColor(mContext,R.color.cb1b3bd));
            tv_one.setBackground(ContextCompat.getDrawable(mContext,R.drawable.x3_dialog_bg));
            tv_two.setBackground(null);
        }else {
            isOne = false;
            tv_two.setTextColor(ContextCompat.getColor(mContext,R.color.c333));
            tv_one.setTextColor(ContextCompat.getColor(mContext,R.color.cb1b3bd));
            tv_two.setBackground(ContextCompat.getDrawable(mContext,R.drawable.x3_dialog_bg));
            tv_one.setBackground(null);
        }
    }

    IOnSwitch iOnSwitch;

    public void setiOnSwitch(IOnSwitch iOnSwitch) {
        this.iOnSwitch = iOnSwitch;
    }

    public interface IOnSwitch{
        void iOnSwitchOne();

        void iOnSwitchTwo();
    }
}
